// 进行路由配置
import Vue from 'vue'
import VueRouter from 'vue-router'

import Home from '../pages/Home/Home.vue'
import Search from '../pages/Search'
import Login from '../pages/Login/Login.vue'
import Register from '../pages/Register/Register.vue'

//使用插件
Vue.use(VueRouter)

// 先把VueRouter原型对象的push先保存一份
let orginPush = VueRouter.prototype.push
let orginReplace = VueRouter.prototype.replace

// 重写push|replace方法
//第一个参数为告诉原生的push方法往那个路由组件跳转
//第二个参数为成功的回调
//第三个参数为失败的回调
VueRouter.prototype.push = function(location,resolve, reject){
    if(resolve && reject){
        orginPush.call(this,location,resolve,reject) // 改变orginPush的指向,call()方法的功能
    }else{
        orginPush.call(this,location,()=>{},()=>{})
    }
}

VueRouter.prototype.replace = function(location,resolve,reject){
    if(resolve && reject){
        orginReplace.call(this,location,resolve,reject)
    }else{
        orginReplace.call(this,location,()=>{},()=>{})
    }
}


// 路由配置
export default new VueRouter({
    routes: [
        {
            path:"/home",
            component:Home,
            meta: {show:true} // 添加一个路由的元信息
        },
        {
            name:'search',
            path:"/search/:keyword?",
            component: Search,
            meta:{show:false}
        },
        {
            path:"/login",
            component:Login,
            meta:{show:true}
        },
        {
            path:'/register',
            component:Register,
            meta:{show:false}
        },
        // 进行重定向操作，在用户访问界面的时候，默认定向到首页
        {
            path:'/',
            redirect: '/home'
        }
    ]
})